package cn.dong.edu.controller.admin;


import cn.dong.edu.domain.Course;
import cn.dong.edu.domain.Subject;
import cn.dong.edu.domain.dto.SubjectRecordVo;
import cn.dong.edu.domain.dto.SubjectVo;
import cn.dong.edu.service.impl.SubjectServiceImpl;
import cn.dong.api.domain.CommonResult;
import cn.dong.edu.service.CourseService;
import cn.dong.base.handler.CustomException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author You-Dong
 * @since 2021-02-04
 */
@RestController
@RequestMapping("/edu/subject")
//@CrossOrigin
public class SubjectController {

    @Autowired
    private SubjectServiceImpl subjectService;
    @Autowired
    private CourseService courseService;

    /**
     * poi使用excel批量导入科目到数据库中,科目分类（包含一级分类和二级分类）
     * @param file
     * @return
     */
    @PostMapping("/importSubject")
    public CommonResult importSubject(MultipartFile file) {
        SubjectRecordVo list = subjectService.importSubject(file);
        System.out.println(list);
        return CommonResult.ok().data(list);
    }

    /**
     * 修改
     * @param subject
     * @return
     */
    @PutMapping("/update")
    public CommonResult update(@RequestBody Subject subject){
        if (subjectService.update(subject, null)) {
            return CommonResult.ok();
        }
        return CommonResult.error();
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult deleteOne(@PathVariable Long id) {
        // 查询该分类是否有课程在引用
        QueryWrapper<Course> wrapper = new QueryWrapper<Course>();
        wrapper.eq("subject_id", id);
        if (courseService.count(wrapper) > 0) {
            throw new CustomException(40000, "该分类被其他课程所使用，不能删除");
        }
        if (subjectService.removeById(id)) {
            return CommonResult.ok();
        }
        return CommonResult.error();
    }

    /**
     * 查询全部的一级科目，并且查询一级科目对应下的二级科目，封装到subjectVo
     * @return
     */
    @GetMapping("/findAll")
    public CommonResult findAll() {
        List<SubjectVo> list = subjectService.findAll();
        return CommonResult.ok().data(list);
    }


    /**
     * 创建
     * @param subject
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody Subject subject) {
        // 创建前判断该科目是否存在
        QueryWrapper<Subject> wrapper = new QueryWrapper<Subject>();
        wrapper.eq("title", subject.getTitle());
        Subject result = subjectService.getOne(wrapper);
        if (result != null) {
            return CommonResult.error().message("该科目已经存在了，不需要添加了！");
        }
        if (subjectService.save(subject)) {
            return CommonResult.ok();
        }
        return CommonResult.error();
    }

    /**
     * 根据二级分类id，查询一级分类id
     * @param id
     * @return
     */
    @GetMapping("/findOneByTwo/{id}")
    public CommonResult findOneIdByTwo(@PathVariable String id){
        String result = subjectService.findOneIdByTwo(id);
        return CommonResult.ok().data(result);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public CommonResult findById(@PathVariable String id){

        Subject subject = subjectService.getById(id);
        return CommonResult.ok().data(subject);
    }
}

